﻿using AIVision.UI.ViewModels;
using LiveCharts;
using LiveCharts.Definitions.Charts;
using LiveCharts.Wpf;
using LiveCharts.Wpf.Charts.Base;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static ImTools.ImMap;

namespace AIVision.UI.Views
{
    /// <summary>
    /// DataView.xaml 的交互逻辑
    /// </summary>
    public partial class DataView : UserControl
    {
        private readonly DataViewModel dataViewModel;

        public DataView(DataViewModel dataViewModel, IEventAggregator eventAggregator)
        {
            InitializeComponent();
            this.dataViewModel = dataViewModel;
            pc1.Series = dataViewModel.Series;
            eventAggregator.GetEvent<UpdatePieSericesEvent>().Subscribe(UpdateUi, ThreadOption.UIThread);
        }
        private void UpdateUi()
        {
            Dispatcher.Invoke(() =>
            {
                dataViewModel.Series.Clear();

                foreach(var v in dataViewModel.SummaryData.DefectCount)
                {
                    dataViewModel.Series.Add(new PieSeries
                    {
                        Title = v.Key,
                        Values = new ChartValues<double> { v.Value },
                        DataLabels = true,
                        LabelPoint = chartPoint => string.Format("{0} ({1:P})", v.Key, chartPoint.Participation)
                    });
                }
            });
        }

    }
}
